﻿using CID.Domain.Infrastructure;
using NHibernate;
using CID.Domain.Development;

namespace CID.Domain.Persistence.Tests.Infrastructure
{
    static class DataProvider
    {
        #region DeploymentAgent
        public static DeploymentAgent CreateDeploymentAgent(string name = "Deployment Agent Name", string address = "Deployment Agent Address")
        {
            return new DeploymentAgent(name, address);
        }

        public static IDeploymentAgent GetDeploymentAgent(ISession session, IDeploymentAgent deploymentAgentData)
        {
            return session.Get<DeploymentAgent>(deploymentAgentData.Id);
        }
        #endregion

        #region PackageType
        public static PackageType CreatePackageType(string name = "Package Type Name", string description = "Package Type Description")
        {
            return new PackageType(name, description);
        }

        public static PackageType GetPackageType(ISession session, IPackageType packageTypeData)
        {
            return session.Get<PackageType>(packageTypeData.Id);
        }
        #endregion

        #region Package
        public static Package CreatePackage(ISession session, IPackageType packageType, IVersion version, byte[] content = null, string comment = "This is the Package")
        {
            return CreatePackage(GetPackageType(session, packageType), Development.DataProvider.GetVersion(session, version), content ?? new byte[] { 1, 2, 3 }, comment);
        }

        public static Package CreatePackage(IPackageType packageType, IVersion version, byte[] content, string comment)
        {
            return (Package)packageType.CreatePackage(version, content, comment);
        }

        public static Package GetPackage(ISession session, IPackage packageData)
        {
            return session.Get<Package>(packageData.Id);
        }
        #endregion

        #region TemplateType
        public static TemplateType CreateTemplateType(string name = "Template Type Name", string description = "Template Type Description", string templateContentEditorConverterType = "TemplateContentConverter", string templateContentEditorViewName = "TemplateContentEditor")
        {
            return new TemplateType(name, description, templateContentEditorConverterType, templateContentEditorViewName);
        }

        private static TemplateType GetTemplateType(ISession session, TemplateType templateType)
        {
            return session.Get<TemplateType>(templateType.Id);
        }
        #endregion

        #region Template
        public static ActivationTemplate CreateActivationTemplate(ISession session, TemplateType templateType, string name = "Template Name", string description = "TemplateDescription", byte[] content = null)
        {
            return CreateActivationTemplate(name, description, content ?? new byte[] { 1, 2, 3 }, GetTemplateType(session, templateType));
        }

        public static ActivationTemplate CreateActivationTemplate(string name, string description, byte[] content, TemplateType templateType)
        {
            return (ActivationTemplate)templateType.CreateActivationTemplate(name, description, content);
        }

        public static DeactivationTemplate CreateDeactivationTemplate(ISession session, TemplateType templateType, string name = "Template Name", string description = "TemplateDescription", byte[] content = null)
        {
            return CreateDeactivationTemplate(name, description, content ?? new byte[] { 1, 2, 3 }, GetTemplateType(session, templateType));
        }

        public static DeactivationTemplate CreateDeactivationTemplate(string name, string description, byte[] content, TemplateType templateType)
        {
            return (DeactivationTemplate)templateType.CreateDeactivationTemplate(name, description, content);
        }

        public static DeploymentTemplate CreateDeploymentTemplate(ISession session, TemplateType templateType, string name = "Template Name", string description = "TemplateDescription", byte[] content = null)
        {
            return CreateDeploymentTemplate(name, description, content ?? new byte[] { 1, 2, 3 }, GetTemplateType(session, templateType));
        }

        public static DeploymentTemplate CreateDeploymentTemplate(string name, string description, byte[] content, TemplateType templateType)
        {
            return (DeploymentTemplate)templateType.CreateDeploymentTemplate(name, description, content);
        }
        
        public static T GetTemplate<T>(ISession session, ITemplate templateData) where T : Template
        {
            return session.Get<T>(templateData.Id);
        }
        #endregion

        #region Parameter
        public static Parameter GetParameter(ISession session, IParameter parameterData)
        {
            return session.Get<Parameter>(parameterData.Id);
        }

        public static Parameter CreateParameter(ISession session, Template template, string name = "Parameter Name", string description = "Parameter Description", ParameterType type = ParameterType.String)
        {
            return CreateParameter(GetTemplate<Template>(session, template), name, description, type);
        }

        public static Parameter CreateParameter(Template template, string name = "Parameter Name", string description = "Parameter Description", ParameterType type = ParameterType.String)
        {
            return (Parameter)template.CreateParameter(name, description, type);
        }
        #endregion
    }
}
